/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web.restaurant.front;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.boxin.ims.modules.wechat.entity.restaurant.Food;
import com.boxin.ims.modules.wechat.entity.restaurant.FoodCategory;
import com.boxin.ims.modules.wechat.entity.restaurant.Restaurant;
import com.boxin.ims.modules.wechat.service.restaurant.FoodCategoryService;
import com.boxin.ims.modules.wechat.service.restaurant.FoodService;
import com.boxin.ims.modules.wechat.service.restaurant.RestaurantService;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;

/**
 * 食品,菜品Controller
 * @author Jakemanse
 * @version 2013-11-14
 */
@Controller
@RequestMapping(value = Global.FRONT_PATH+"/wechat/restaurant/food")
public class FoodFrontController extends BaseController {

	private final Logger logger = Logger.getLogger(FoodFrontController.class);
	@Autowired
	private FoodService foodService;
	@Autowired
	private FoodCategoryService foodCategoryService;
	
	
	@Autowired
	private RestaurantService restaurantService;
	
	@ModelAttribute
	public Food get(@RequestParam(required=false) Long id) {
		if (id != null){
			return foodService.get(id);
		}else{
			return new Food();
		}
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-15  上午8:57:00
	 * @function <p> 菜品种类 </p>
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value =  "category" )
	public String categoryList(HttpServletRequest request, HttpServletResponse response, Model model) {
		String uid = request.getParameter("uid");
		if(StringUtils.isNotEmpty(uid)){
			try {
				List<FoodCategory> categoryList =  foodCategoryService.findByUserId(Long.parseLong(uid));
				model.addAttribute("categoryList", categoryList);
				
			} catch (Exception e) {
			}
		}
		
		
		return "modules/wechat/restaurant/front/categoryList";
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-22  下午10:47:56
	 * @function <p> 餐厅下所有的菜品种类 </p>
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value =  "cat" )
	public String categoryByRestaurantList(HttpServletRequest request, HttpServletResponse response, Model model) {
		String uid = request.getParameter("uid");
		String rid = request.getParameter("rid");	//Restaurant ID
		if(StringUtils.isNotEmpty(uid)){
			try {
				
				List<FoodCategory> categoryList = foodCategoryService.findByRestauranttId(Long.parseLong(rid));
				Restaurant restaurant  = restaurantService.get(Long.parseLong(rid));
				model.addAttribute("categoryList", categoryList);
				model.addAttribute("restaurant", restaurant);
				
			} catch (Exception e) {
			}
		}
		
		
		return "modules/wechat/restaurant/front/categoryList";
	}
	
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-15  上午8:56:44
	 * @function <p> 所有菜品 </p>
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value =  "list" )
	public String foodList(HttpServletRequest request, HttpServletResponse response, Model model) {
		
		String uid = request.getParameter("uid");
		String categoryId = request.getParameter("cid");
		
		Food food = new Food();
		if(StringUtils.isNotEmpty(uid)){
			User user =  new User();
			user.setId(Long.parseLong(uid));
			food.setUser(user);
			
		}
		
		if(StringUtils.isNotEmpty(categoryId)){
			try {
				
				FoodCategory foodCategory = foodCategoryService.get(Long.parseLong(categoryId));
				foodCategory.setId(Long.parseLong(categoryId));
				food.setCategory(foodCategory);
				
				model.addAttribute("foodCategory", foodCategory);
			} catch (Exception e) {
				
			}
		}
		
        Page<Food> page = foodService.find(new Page<Food>(request, response), food); 
        model.addAttribute("page", page);
        
        
		return "modules/wechat/restaurant/front/foodList";
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-15  上午12:19:24
	 * @function <p> 查看菜品明细 </p>
	 * @param id
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	
	@RequestMapping(value = "view/{id}")
	public String viewDetail(@PathVariable("id") Long id,  HttpServletRequest request, HttpServletResponse response, Model model) {
		
		Food food = foodService.get(id);
		if(food != null){
//			Restaurant restaurant =	restaurantService.getByUserId(food.getUser().getId());
//			model.addAttribute("restaurant", restaurant);
		}
		model.addAttribute("food", food);
		return "modules/wechat/restaurant/front/foodView";
	}
	
	
	



}
